package com.inspector.app.di.util.geomagnetism

import com.inspector.app.util.CommonUtil
import com.mapsforge.map.EfbLatLngPoint
import java.util.*

/**
 * 磁差数据工具类
 * thanks
 * https://www.ngdc.noaa.gov/geomag/data.shtml
 * https://github.com/kanchudeep/Geomagnetism-Java
 */
class GeomagnetismUtils {

    fun getGeomagnetismByPoint(point: EfbLatLngPoint): String {
        val geomagnetism = Geomagnetism(point.lng, point.lat)
        geomagnetism.calculate(point.lng, point.lat)
        return CommonUtil.formatDouble(geomagnetism.declination, 2)
    }

    fun getGeomagnetismByPoint(lng: Double, lat: Double): Double {
        val geomagnetism = Geomagnetism(lng, lat)
        geomagnetism.calculate(lng, lat)
        return geomagnetism.declination
    }


    fun getGeomagnetismByPoint(point: EfbLatLngPoint, altitude: Double): String {
        val geomagnetism = Geomagnetism(point.lng, point.lat)
        geomagnetism.calculate(point.lng, point.lat, altitude)
//        val altitude = ElevationUtils.getInstance().getOfflineElevation(point)
        return CommonUtil.formatDouble(geomagnetism.declination, 2)
    }

    fun getGeomagnetismByPoint(
        point: EfbLatLngPoint,
        altitude: Double,
        calendar: GregorianCalendar
    ): String {
        val geomagnetism = Geomagnetism(point.lng, point.lat)
//        val calendar = GregorianCalendar()
//        calendar.set(2022, 8, 4)
        geomagnetism.calculate(point.lng, point.lat, altitude, calendar)
//        val altitude = ElevationUtils.getInstance().getOfflineElevation(point)
        return CommonUtil.formatDouble(geomagnetism.declination, 2)
    }


    companion object {
        @JvmStatic
        val instance: GeomagnetismUtils by lazy {
            GeomagnetismUtils()
        }
    }
}